﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ZX.Report;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.DBModule;

namespace ZX.Web.UI.Controls
{
    public partial class ChartReportView : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string funId = GetStringFromQueryString("functionID", string.Empty);
                if (funId == string.Empty) return;
                SysChartReport sysChartReport = new SysChartReport();
                BaseBussnies bll = new BaseBussnies(sysChartReport);
                IList<BaseEntity> lists = bll.GetEntitysByCondition("MenuId='" + funId + "' order by SeqNum asc");
                DataSet dataSources = new DataSet();
                Dictionary<int, string> titles = new Dictionary<int, string>();
                Dictionary<int, int> widths = new Dictionary<int, int>();
                Dictionary<int, int> heights = new Dictionary<int, int>();
                Dictionary<int, string> viewTypes = new Dictionary<int, string>();
                Dictionary<int, int> xy = new Dictionary<int, int>();
                List<int> seqNum = new List<int>();
                SysReportQueryParameter rpQueryParam = new SysReportQueryParameter();
                BaseBussnies rpQueryParamBll = new BaseBussnies(rpQueryParam);
                int i = 0; //定义循环次数
                IList<BaseEntity> baseEntity = null; //定义接收参数变量 
                foreach (BaseEntity entity in lists)
                {
                    SysChartReport r = (SysChartReport)entity;
                    DataTable dt = null;
                    string sql = r.SQL;
                    //判断该报表有没有查询参数
                    IList<BaseEntity> rpQueryParamList = rpQueryParamBll.GetEntitysByCondition("ChartReportId='" + r.ID.ToString() + "'");
                    //取出查询参数的值并在SQL中替换
                    if (rpQueryParamList != null)
                    {
                        if (lists.Count > 1)
                        {
                            baseEntity = rpQueryParamList;
                        }
                        foreach (BaseEntity rpEntity in rpQueryParamList)
                        {
                            SysReportQueryParameter tempRpQP = rpEntity as SysReportQueryParameter;
                            string paramValue = GetStringFromQueryString(tempRpQP.ParameterName, string.Empty); //参数值
                            if (paramValue == string.Empty && sql.Contains(tempRpQP.ParameterName)) return;
                            sql = sql.Replace(tempRpQP.ParameterName, paramValue);//替换参数
                        }
                    }
                    else 
                    {
                        if (sql.ToLower()[0] == 'e' && baseEntity !=null) //判断没参数存储过程
                        {
                            foreach (BaseEntity rpEntity in baseEntity)
                            {
                                SysReportQueryParameter tempRpQP = rpEntity as SysReportQueryParameter;
                                string paramValue = GetStringFromQueryString(tempRpQP.ParameterName, string.Empty); //参数值
                                if (paramValue == string.Empty && sql.Contains(tempRpQP.ParameterName)) return;
                                sql = sql.Replace(tempRpQP.ParameterName, paramValue);//替换参数
                            }
                        }
                    }
                    if (sql.ToLower()[0]=='v') //视图
                    {
                        dt = bll.GetDataBySqlComm("select * from " + sql); 
                    }
                    else //sql语句
                    {
                        sql = sql.Replace("[B]", "'");
                        dt = bll.GetDataBySqlComm(sql); 
                    }
                    titles[r.SeqNum] = r.ChartTitle;
                    widths[r.SeqNum] = r.Width;
                    heights[r.SeqNum] = r.Height;
                    viewTypes[r.SeqNum] = r.ChartType;
                    xy[r.SeqNum] = r.XY;
                    seqNum.Add(r.SeqNum);
                    DataTable source = dt.Copy();
                    source.TableName = r.SeqNum.ToString();
                    dataSources.Tables.Add(source);
                }
                if (viewTypes[1] == "list")
                {
                    ListParams paramsFactory = (ListParams)ParamsFactory.GetInstance(ReportType.List, dataSources);
                    ReportFactory reportFactory = ReportFactory.GetInstance(paramsFactory);
                    this.ReportViewer.Report = reportFactory.GetReport();
                }
                else
                {
                    ChartParams paramsFactory = (ChartParams)ParamsFactory.GetInstance(ReportType.Chart, dataSources);
                    paramsFactory.Title = titles;
                    paramsFactory.Width = widths;
                    paramsFactory.Height = heights;
                    paramsFactory.XYType = xy;
                    paramsFactory.SeqNum = seqNum.ToArray();
                    paramsFactory.ChartViewType = viewTypes;
                    ReportFactory reportFactory = ReportFactory.GetInstance(paramsFactory);
                    this.ReportViewer.Report = reportFactory.GetReport();
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "script", "<script language='javascript'>alert('生成报表出现错误:" + ex.Message + "');</script>");
            }
        }
    }
}
